package com.etcbase.dcs.controller.login;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.etcbase.dcs.DcsApp;
import com.etcbase.dcs.R;
import com.etcbase.dcs.controller.lookup.LookupService;
import com.etcbase.dcs.controller.main.MainActivity;
import com.etcbase.dcs.model.lookup.LookupServiceData;
import com.etcbase.dcs.model.lookup.Lookupable;

public class LoginActivity extends Activity {
	public static final String TAG = "LoginActivity";

	private static final String validUserName = "test";
	private static final String validPassword = "test";

	private EditText userNameEditText;
	private EditText passwordEditText;
	private ToggleButton rememberToggleButton;
	private SharedPreferences userDetailsSharedPreferences;

	public DcsApp getApp() {
		return (DcsApp) getApplication();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_panel);

		// hide the keyboard when not needed
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		// enter true to go directly to the main activity to test main gui
		// quickly
		if (getApp().DEVELOPMENT_SKIP_LOGIN) {
			startActivity(new Intent(LoginActivity.this, MainActivity.class));
			finish();
		}

		userNameEditText = (EditText) findViewById(R.id.loginEditTextUserName);
		passwordEditText = (EditText) findViewById(R.id.loginEditTextPassword);
		rememberToggleButton = (ToggleButton) findViewById(R.id.loginToggleButtonRemember);

		userDetailsSharedPreferences = getSharedPreferences("userdetails",
				MODE_PRIVATE);

		// ENTER-BUTTON
		findViewById(R.id.loginButtonEnter).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {

						// check user-name and password
						if (isValidUser()) {

							// save the valid user-data if requested in
							// preferences
							Editor editor = userDetailsSharedPreferences.edit();
							if (rememberToggleButton.isChecked()) {
								editor.putString("username", userNameEditText
										.getText().toString());
								editor.putString("password", passwordEditText
										.getText().toString());
							} else {
								editor.putString("username", "");
								editor.putString("password", "");
							}
							editor.commit();

							InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
							inputMethodManager.hideSoftInputFromWindow(
									passwordEditText.getWindowToken(), 0);

							// start the main-activity
							startActivity(new Intent(LoginActivity.this,
									MainActivity.class));

						} else {
							AlertDialog.Builder builder = new AlertDialog.Builder(
									LoginActivity.this);
							builder.setCancelable(true);
							builder.setTitle("Cannot log in!");
							builder.setPositiveButton("OK",
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.dismiss();
										}
									});
							builder.setMessage("Invalid user-name and password!");
							builder.setInverseBackgroundForced(true);
							AlertDialog alert = builder.create();
							alert.show();
						}
					}
				});

		// SIGN-UP
		findViewById(R.id.loginButtonSignUp).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						toast("Sign up...");
					}
				});

		// FORGOT PASSWORD
		findViewById(R.id.loginButtonForgotPassword).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						toast("Forgot password...");
					}
				});
	}

	private void sleep(int i) {
		// TODO Auto-generated method stub

	}

	void toast(String text) {
		Toast.makeText(LoginActivity.this, text, Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "onStart");
	}

	@Override
	protected void onResume() {
		super.onStart();
		Log.d(TAG, "onResume");

		// clear the fields
		String username = userDetailsSharedPreferences
				.getString("username", "");
		String password = userDetailsSharedPreferences
				.getString("password", "");

		userNameEditText.setText(username);
		passwordEditText.setText(password);

		if ((!username.equals("")) && (!password.equals(""))) {
			rememberToggleButton.setChecked(true);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "onStop");
	}

	private boolean isValidUser() {
		String userName = userNameEditText.getText().toString();
		String password = passwordEditText.getText().toString();
		return (userName.equals(validUserName) && password
				.equals(validPassword));
	}

}
